package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.*;
import com.abaaba.project2.bean.bo.OrderSubmitBo;
import com.abaaba.project2.bean.bo.WxOrderCommentsBo;
import com.abaaba.project2.bean.bo.WxOrderListBo;
import com.abaaba.project2.bean.vo.*;
import com.abaaba.project2.mapper.CommentMapper;
import com.abaaba.project2.mapper.OrderGoodsMapper;
import com.abaaba.project2.mapper.OrderMapper;
import com.abaaba.project2.service.WxOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.System;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WxOrderServiceImpl implements WxOrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;
    @Autowired
    CommentMapper commentMapper;


    @Override
    public WxOrderListVo queryOrderListByStatus(WxOrderListBo wxOrderListBo, Integer userId) {
        PageHelper.startPage(wxOrderListBo.getPage(), wxOrderListBo.getSize());
        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();
        if (wxOrderListBo.getShowType() != null && (!wxOrderListBo.getShowType().equals(0))) {
            List<Short> shorts = WxOrderServiceImpl.transferStatus(wxOrderListBo.getShowType());
            if (!shorts.isEmpty()) {
                criteria.andOrderStatusIn(shorts);
            }
        }

        criteria.andUserIdEqualTo(userId).andDeletedEqualTo(false);
        List<Order> orders = orderMapper.selectByExample(orderExample);
        List<WxOrderDataVo> wxOrderDataVos = new ArrayList<>();
        for (Order order : orders) {
            WxOrderDataVo wxOrderDataVo = new WxOrderDataVo();
            wxOrderDataVo.setId(order.getId());
            wxOrderDataVo.setOrderStatus(order.getOrderStatus());
            wxOrderDataVo.setActualPrice(order.getActualPrice());
            wxOrderDataVo.setOrderSn(order.getOrderSn());
            WxOrderServiceImpl.isGroupon(order, wxOrderDataVo);

            OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
            orderGoodsExample.createCriteria().andOrderIdEqualTo(order.getId());
            List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
            List<WxOrderGoodsInfo> wxOrderGoodsLists = new ArrayList<>();
            for (OrderGoods orderGood : orderGoods) {
                WxOrderGoodsInfo wxOrderGoodsInfo = new WxOrderGoodsInfo();
                wxOrderGoodsInfo.setId(orderGood.getId());
                wxOrderGoodsInfo.setGoodsName(orderGood.getGoodsName());
                wxOrderGoodsInfo.setNumber(orderGood.getNumber());
                wxOrderGoodsInfo.setPicUrl(orderGood.getPicUrl());
                wxOrderGoodsLists.add(wxOrderGoodsInfo);
            }
            wxOrderDataVo.setGoodsList(wxOrderGoodsLists);
            wxOrderDataVos.add(wxOrderDataVo);
        }

        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        WxOrderListVo wxOrderListVo = new WxOrderListVo();
        wxOrderListVo.setTotalPages(orderPageInfo.getPageNum());
        wxOrderListVo.setData(wxOrderDataVos);
        wxOrderListVo.setCount(wxOrderListVo.getData().size());

        return wxOrderListVo;
    }

    @Override
    public WxOrderDetailVo queryOrderDetail(Integer id) {
        //默认指定一位用户 模仿登录
        Order order = orderMapper.selectByPrimaryKey(id);
        WxOrderDetailInfoVo wxOrderDetailInfoVo = new WxOrderDetailInfoVo();

        wxOrderDetailInfoVo.setOrderStatus(order.getOrderStatus());
        wxOrderDetailInfoVo.setConsignee(order.getConsignee());
        wxOrderDetailInfoVo.setAddress(order.getAddress());
        wxOrderDetailInfoVo.setAddTime(order.getAddTime());
        wxOrderDetailInfoVo.setOrderSn(order.getOrderSn());
        wxOrderDetailInfoVo.setActualPrice(order.getActualPrice());
        wxOrderDetailInfoVo.setMobile(order.getMobile());
        wxOrderDetailInfoVo.setGoodsPrice(order.getGoodsPrice());
        wxOrderDetailInfoVo.setCouponPrice(order.getCouponPrice());
        wxOrderDetailInfoVo.setId(order.getId());
        wxOrderDetailInfoVo.setFreightPrice(order.getFreightPrice());

        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        orderGoodsExample.createCriteria().andOrderIdEqualTo(id).andDeletedEqualTo(false);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);


        WxOrderDetailVo wxOrderDetailVo = new WxOrderDetailVo();
        wxOrderDetailVo.setOrderInfo(wxOrderDetailInfoVo);
        wxOrderDetailVo.setOrderGoods(orderGoods);

        return wxOrderDetailVo;
    }

    @Override
    public Integer updateOrderStatus(Integer orderId, Short orderStatus, Integer userId) {
        Order order = new Order();
        order.setUserId(userId);
        order.setId(orderId);
        order.setOrderStatus(orderStatus);
        order.setUpdateTime(new Date(System.currentTimeMillis()));
        order.setEndTime(order.getUpdateTime());
        if (orderStatus.equals(((short) 401))) {
            //    待评论数 +1
            orderMapper.addComments(orderId);
            order.setConfirmTime(order.getUpdateTime());
        }
        return orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public Integer updateOrderDeleted(Integer orderId, Integer userId) {
        Order order = new Order();
        order.setId(orderId);
        order.setUserId(userId);
        order.setUpdateTime(new Date(System.currentTimeMillis()));
        order.setEndTime(order.getUpdateTime());
        order.setDeleted(true);
        return orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public Integer insertComments(WxOrderCommentsBo wxOrderCommentsBo, Integer userId) {
        Comment comment = new Comment();
        comment.setType(((byte) 0));
        comment.setUserId(userId);
        comment.setContent(wxOrderCommentsBo.getContent());
        comment.setHasPicture(wxOrderCommentsBo.getHasPicture());
        comment.setPicUrls(wxOrderCommentsBo.getPicUrls());
        comment.setStar(wxOrderCommentsBo.getStar());
        comment.setAddTime(new Date(System.currentTimeMillis()));
        comment.setUpdateTime(comment.getAddTime());
        comment.setDeleted(false);
        OrderGoods orderGoods = orderGoodsMapper.selectByPrimaryKey(wxOrderCommentsBo.getOrderGoodsId());
        comment.setValueId(orderGoods.getGoodsId());//暂时假定value—id为goods—id

        Order order = new Order();
        order.setId(orderGoods.getOrderId());
        order.setUserId(userId);
        order.setOrderStatus(((short) 403));
        order.setUpdateTime(new Date(System.currentTimeMillis()));
        orderMapper.decComments(orderGoods.getOrderId());
        orderMapper.updateByPrimaryKeySelective(order);

        orderGoods.setComment(comment.getId());
        orderGoodsMapper.updateByPrimaryKeySelective(orderGoods);

        return commentMapper.insertSelective(comment);
    }

    @Override
    public OrderGoods queryOrderGoods(Integer orderId, Integer goodsId) {
        //user
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(orderId).andGoodsIdEqualTo(goodsId).andDeletedEqualTo(false);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
        if (orderGoods.size() != 1) {
            return null;
        } else
            return orderGoods.get(0);

    }

    @Override
    public Integer insertOrder(OrderSubmitBo orderSubmitBo,Integer userId) {

        return null;
    }

    private static void isGroupon(Order order, WxOrderDataVo wxOrderDataVo) {

        if (order.getGrouponPrice() == null || order.getGrouponPrice().equals(0.00)) {
            wxOrderDataVo.setGroupin(false);
        } else {
            wxOrderDataVo.setGroupin(true);
        }
    }

    private static List<Short> transferStatus(Integer showType) {

        ArrayList<Short> shorts = new ArrayList<>();
        if (showType.equals(1)) {
            shorts.add(Short.parseShort("101"));
        } else if (showType.equals(2)) {
            shorts.add(Short.parseShort("201"));
        } else if (showType.equals(3)) {
            shorts.add(Short.parseShort("301"));
        } else if (showType.equals(4)) {
            shorts.add(Short.parseShort("401"));
            shorts.add(Short.parseShort("402"));
            shorts.add(Short.parseShort("403"));
        }
        return shorts;
    }
}
